//
//  HXWebImageView.m
//  HXTClient
//
//  Created by lemon on 14-6-9.
//  Copyright (c) 2014年 easaa. All rights reserved.
//

#import "HXWebImageView.h"
//#import "UIImageView+UIActivityIndicatorForSDWebImage.h"

@interface HXWebImageView () <UIScrollViewDelegate, UIGestureRecognizerDelegate>
{
    UIScrollView *_scrollView;
    UIImageView *_imageView;
    NSString *_strImageUrl;
}

@end

@implementation HXWebImageView

- (instancetype)initWebImageViewWithImageUrl:(NSString *)strImageUrl
{
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight + 20)];
    if (self)
    {
        self.layer.backgroundColor = [UIColor colorWithRed:28.0f/255.0f
                                                     green:28.0f/255.0f
                                                      blue:28.0f/255.0f
                                                     alpha:1.f].CGColor;
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        //[self addTarget:self action:@selector(touchSelfAction) forControlEvents:UIControlEventTouchUpInside];
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0., 0.);
        
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.delegate = self;
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [self addSubview:_scrollView];
        
        _imageView = [[UIImageView alloc] initWithFrame:_scrollView.bounds];
        _imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.userInteractionEnabled = YES;
        [_scrollView addSubview:_imageView];
        
        _strImageUrl = strImageUrl;
    }
    return self;
}

- (void)touchSelfAction
{
    [self hide];
}

-  (void)hide
{
    [UIView animateWithDuration:1.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformScale(CGAffineTransformRotate(CGAffineTransformIdentity, 2*M_PI/2), 0.f, 0.f);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)show
{
    __weak typeof(_scrollView) weakScrollView = _scrollView;
    __weak typeof(_imageView) weakImageView = _imageView;
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.5f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, 1.f);
    } completion:^(BOOL finished) {
        
     
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_strImageUrl]
                   placeholderImage:[UIImage imageNamed:@"bg.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                              
        }];


        float minimumScale = CGRectGetWidth(weakScrollView.frame)/CGRectGetWidth(weakImageView.frame);
        weakScrollView.minimumZoomScale = minimumScale;
        weakScrollView.maximumZoomScale = minimumScale*3.f;
        [weakScrollView setZoomScale:minimumScale];
        
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:weakSelf                                                                                                                action:@selector(handleDoubleTap:)];
        [doubleTapGesture setNumberOfTapsRequired:1];
        [weakImageView addGestureRecognizer:doubleTapGesture];
    
     }];
    
//
//           [_imageView sd_setImageWithURL:[NSURL URLWithString:_strImageUrl] placeholderImage:[UIImage imageNamed:@"bg.png"]
//                                  options:SDWebImageRetryFailed
//                                completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//                                    
//                                    float minimumScale = CGRectGetWidth(weakScrollView.frame)/CGRectGetWidth(weakImageView.frame);
//                                    weakScrollView.minimumZoomScale = minimumScale;
//                                    weakScrollView.maximumZoomScale = minimumScale*3.f;
//                                    [weakScrollView setZoomScale:minimumScale];
//                                    
//                                    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:weakSelf
//                                                                                                                       action:@selector(handleDoubleTap:)];
//                                    [doubleTapGesture setNumberOfTapsRequired:1];
//                                    [weakImageView addGestureRecognizer:doubleTapGesture];
//                                } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    }];

        
//        
//        [_imageView setImageWithURL:[NSURL URLWithString:_strImageUrl] placeholderImage:[UIImage imageNamed:@"guanggao_bg.png"]
//                            options:SDWebImageRetryFailed | SDWebImageLowPriority
//                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
//                              
//            float minimumScale = CGRectGetWidth(weakScrollView.frame)/CGRectGetWidth(weakImageView.frame);
//            weakScrollView.minimumZoomScale = minimumScale;
//            weakScrollView.maximumZoomScale = minimumScale*3.f;
//            [weakScrollView setZoomScale:minimumScale];
//                              
//          UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:weakSelf
//                                                                                             action:@selector(handleDoubleTap:)];
//          [doubleTapGesture setNumberOfTapsRequired:1];
//          [weakImageView addGestureRecognizer:doubleTapGesture];
//        } usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    }];
}

- (void)handleDoubleTap:(UIGestureRecognizer *)gesture
{
    [self hide];
//    float newScale = _scrollView.zoomScale * 1.5;
//    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
//    [_scrollView zoomToRect:zoomRect animated:YES];
}

//- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
//{
//    CGRect zoomRect;
//    zoomRect.size.height = self.frame.size.height / scale;
//    zoomRect.size.width  = self.frame.size.width  / scale;
//    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
//    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
//    return zoomRect;
//}

#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    [scrollView setZoomScale:scale animated:NO];
}

@end
